const path = require("path");
const clear = require("rollup-plugin-clear");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const { babel } = require("@rollup/plugin-babel");
const commonjs = require("@rollup/plugin-commonjs");
const typescript = require("rollup-plugin-typescript2");
const autoprefixer = require("autoprefixer");
const inject = require("@rollup/plugin-inject");
const url = require("@rollup/plugin-url");
const postcss = require("rollup-plugin-postcss");
const input = path.resolve(__dirname, "../src/components/index.tsx");
const outDir = path.resolve(__dirname, "../lib");
const esOutDir = path.resolve(__dirname, "../es");
module.exports = {
  input,
  output: [
    {
      dir: outDir,
      format: "cjs",
      footer: "// powered by h",
      entryFileNames: "[name].js",
      exports: "named",
      preserveModules: true,
    },
    {
      dir: esOutDir,
      format: "es",
      banner: "// welcome to hoo hoo hoo",
      footer: "// powered by hdj",
      entryFileNames: "[name].js",
      exports: "named",
      preserveModules: true,
    },
  ],
  plugins: [
    clear({
      targets: [outDir, esOutDir],
    }),
    url({
      fileName: "[dirname][hash][extname]",
    }),
    postcss({
      extract: "index.css",
      plugins: [
        autoprefixer({
          overrideBrowserslist: [
            "last 10 Chrome versions",
            "last 5 Firefox versions",
            "Safari >= 6",
            "ie> 8",
          ],
        }),
      ],
    }),
    typescript({
      tsconfigOverride: {
        include: [`src/components/**/*`],
      },
    }),
    babel({
      exclude: "node_modules/**",
      extensions: [".ts", ".tsx", ".js", ".jsx"],
      babelHelpers: "runtime",
      presets: [
        [
          "@babel/preset-env",
          {
            targets: {
              chrome: "58",
              ie: "11",
              ios: "11",
            },
            useBuiltIns: "usage",
            corejs: 3,
            modules: false,
          },
        ],
      ],
    }),
    inject({
      React: "react",
    }),
    nodeResolve({
      resolveOnly: ["node_modules"], //定义为模块
    }),
    commonjs({
      include: ["node_modules"],
    }),
  ],
};
